/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ebdesk.controller.dhkp;
import com.ebdesk.controller.dhkp.NumberToText;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/**
 *
 * @author mbenk
 */
public class Lib {
    private static String username;

    public static void setUsername(String user) {
        username = user;
    }

    public static String getUsername() {
        return username;
    }

    public static String decodeMD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger number = new BigInteger(1, messageDigest);
            String hashtext = number.toString(16);
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static String dateFormat(Date visited) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
        FieldPosition pos = new FieldPosition(0);
        StringBuffer empty = new StringBuffer();
        StringBuffer date = sdf.format(visited, empty, pos);
        return date.toString();
    }

    public static String dateFormatYMD(Date visited) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        FieldPosition pos = new FieldPosition(0);
        StringBuffer empty = new StringBuffer();
        StringBuffer date = sdf.format(visited, empty, pos);
        return date.toString();
    }

    public static String dateFormatIndo(Date visited) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        FieldPosition pos = new FieldPosition(0);
        StringBuffer empty = new StringBuffer();
        StringBuffer date = sdf.format(visited, empty, pos);
        String tanggal = date.toString();
        String bulan = tanggal.substring(3, 5);

        if (bulan.equals("01")) {
            bulan = "Januari";
        } else if (bulan.equals("02")) {
            bulan = "Februari";
        } else if (bulan.equals("03")) {
            bulan = "Maret";
        } else if (bulan.equals("04")) {
            bulan = "April";
        } else if (bulan.equals("05")) {
            bulan = "Mei";
        } else if (bulan.equals("06")) {
            bulan = "Juni";
        } else if (bulan.equals("07")) {
            bulan = "Juli";
        } else if (bulan.equals("08")) {
            bulan = "Agustus";
        } else if (bulan.equals("09")) {
            bulan = "September";
        } else if (bulan.equals("10")) {
            bulan = "Oktober";
        } else if (bulan.equals("11")) {
            bulan = "November";
        } else if (bulan.equals("12")) {
            bulan = "Desember";
        }

        return tanggal.substring(0, 2) + " " + bulan + " " + tanggal.substring(6, 10);
    }

    public static String dateFormatIndo2(Date visited) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        FieldPosition pos = new FieldPosition(0);
        StringBuffer empty = new StringBuffer();
        StringBuffer date = sdf.format(visited, empty, pos);
        String tanggal = date.toString();
        String bulan = tanggal.substring(3, 5);

        if (bulan.equals("01")) {
            bulan = "JAN";
        } else if (bulan.equals("02")) {
            bulan = "FEB";
        } else if (bulan.equals("03")) {
            bulan = "MAR";
        } else if (bulan.equals("04")) {
            bulan = "APR";
        } else if (bulan.equals("05")) {
            bulan = "MEI";
        } else if (bulan.equals("06")) {
            bulan = "JUN";
        } else if (bulan.equals("07")) {
            bulan = "JUL";
        } else if (bulan.equals("08")) {
            bulan = "AGU";
        } else if (bulan.equals("09")) {
            bulan = "SEP";
        } else if (bulan.equals("10")) {
            bulan = "OKT";
        } else if (bulan.equals("11")) {
            bulan = "NOV";
        } else if (bulan.equals("12")) {
            bulan = "DES";
        }

        return tanggal.substring(0, 2) + " " + bulan + " " + tanggal.substring(6, 10);
    }

    public static String dateFormatNow() {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        FieldPosition pos = new FieldPosition(0);
        StringBuffer empty = new StringBuffer();
        StringBuffer date = sdf.format(now, empty, pos);
        return date.toString();
    }

//    public static ComponentProperties getProperties() {
//        return EasyConf.getConfiguration("com/ebdesk/openswing/config/config").getProperties();
//    }
//
//    public static ComponentProperties getDbProperties() {
//        return EasyConf.getConfiguration("com/ebdesk/openswing/config/database").getProperties();
//    }

    public static String FormatSPPT(String sppt) {
        String result = sppt.substring(0, 2) + "." + sppt.substring(2, 4) + "." + sppt.substring(4, 7) +
                "." + sppt.substring(7, 10) + "." + sppt.substring(10, 13) + "-" + sppt.substring(13, 17) + "." + sppt.substring(17, 18);
        return result;
    }

    public static String NumberFormat(double value) {
        Locale locale = Locale.GERMAN;
        String formatter = NumberFormat.getNumberInstance(locale).format(value);
        return formatter;
    }
    public static Date String2Date(String str_date) {
        DateFormat formatter;
        Date date = null;
        formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = (Date) formatter.parse(str_date);
        } catch (ParseException ex) {
        }
        return date;
    }

    public static Date Timestamp2Date(String str_date) {
        DateFormat formatter;
        Date date = null;
        formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
        try {
            date = (Date) formatter.parse(str_date);
        } catch (ParseException ex) {
        }
        return date;
    }

    public static String DateFormat(Date date) {
        String Format = "dd-MM-yyyy";
        SimpleDateFormat f;
        f = new SimpleDateFormat(Format);
        return f.format(date);
    }

    public static String DateIndoFormat(Date date) {
        String Format = "dd-MM-yyyy";
        SimpleDateFormat f;
        f = new SimpleDateFormat(Format);
        return f.format(date);
    }

    public static String NumberToText(double value) {
        return NumberToText.convert(value);
    }

    public static String implode(List<String> listData, String separator, boolean isNumeric) {
        String result = "";
        for (int i = 0; i < listData.size(); i++) {
            if (i != 0) {
                result += separator;
            }
            if (isNumeric) {
                result += String.valueOf(listData.get(i));
            } else {
                result += "'" + String.valueOf(listData.get(i)) + "'";
            }
        }

        return result;
    }

    public static String allignRight(String value, int column) {
        int sisa = column - value.length();
        if (sisa < 1) {
            return value;

        } else {
            return Lib.loopChar(" ", sisa - 1) + value;
        }
    }

    public static String allignCenter(String value, int column) {
        int sisa = (column / 2) - (value.length() / 2);
        if (sisa < 1) {
            return value;

        } else {
            return Lib.loopChar(" ", sisa - 1) + value;
        }
    }
    
    public static String allignWrap(String value, int column) {
        int sisa = (column / 2) - (value.length() / 2);
        if (sisa < 1) {
            return value;

        } else {
            return Lib.loopChar(" ", sisa - 1) + value;
        }
    }

    public static String loopChar(String value, int loop) {
        String hasil = "";
        for (int i = 0; i < loop; i++) {
            hasil = hasil + value;
        }
        return hasil;
    }

    public static String CheckJSON(String json) {
        if (json.indexOf("[") == 0) {
            return json;
        } else {
            return "[" + json + "]";
        }
    }

    
}
